package com.fun.mysql;

import com.fun.frame.SourceCode;
import com.fun.profile.SqlConstant;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class MySqlLocal extends SourceCode {
    Connection connection;
    Statement statement;
    static MySqlLocal mySqlLocal = new MySqlLocal();

    /**
     * 获取实例
     *
     * @return
     */
    public static MySqlLocal getInstance() {
        return mySqlLocal;
    }

    /**
     * 私有构造方法
     */
    private MySqlLocal() {
        getConnection();
    }


    /**
     * 初始化连接
     */
    public void getConnection() {
        connection = SqlBase.getConnection(SqlConstant.FAN_SQL_URL, SqlConstant.FAN_USER, SqlConstant.FAN_PASS_WORD);
        statement = SqlBase.getStatement(connection);
    }

    /**
     * 执行sql语句，非query语句，并不关闭连接
     *
     * @param sql
     * @param key
     */
    public void excuteUpdateSql(String sql) {
        SqlBase.excuteUpdateSql(connection, statement, sql);
        mySqlOver();
    }

    /**
     * 查询功能
     *
     * @param sql
     * @return
     */
    public ResultSet excuteQuerySql(String sql) {
        return SqlBase.excuteQuerySql(connection, statement, sql);
    }

    /**
     * 关闭query连接
     */
    public void mySqlOver() {
        SqlBase.mySqlOver(connection, statement);
    }
}
